package org.example.actualcombat.demos.web.modules.pms.controller;


import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.example.actualcombat.demos.web.common.api.CommonPage;
import org.example.actualcombat.demos.web.common.api.CommonResult;
import org.example.actualcombat.demos.web.modules.pms.model.PmsProductCategoryModel;
import org.example.actualcombat.demos.web.modules.pms.model.dto.PmsProductCategoryDto;
import org.example.actualcombat.demos.web.modules.pms.model.dto.ProductCateChildrenDto;
import org.example.actualcombat.demos.web.modules.pms.service.PmsProductCategoryService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/*
* 商品/ 商品分类/ 增删改查
* */
@RestController
@RequestMapping("/productCategory")
public class PmsProductCategoryController {
    @Autowired
    private PmsProductCategoryService pmsProductCategoryService;

    // 查询列表
//    url:'/productCategory/list/'+parentId,
//    method:'get',
//    params:params
    @RequestMapping(value = "/list/{parentId}",method = RequestMethod.GET)
    public CommonResult<CommonPage<PmsProductCategoryModel>> getList(@PathVariable Long parentId, @RequestParam(value = "pageNum",defaultValue = "1") Integer pageNum, @RequestParam(value = "pageSize",defaultValue = "5") Integer pageSize){
        Page  page = pmsProductCategoryService.list(parentId,pageNum, pageSize);
        return CommonResult.success(CommonPage.restPage(page));
    }

    @RequestMapping(value = "/list/withChildren",method = RequestMethod.GET)
    public CommonResult getWithChildren(){
        List<ProductCateChildrenDto> list = pmsProductCategoryService.getWithChildren();
        return  CommonResult.success(list);
    }


































// 创建单个
//    url:'/productCategory/create',
//    method:'post',
//    data:data
    @RequestMapping(value = "/create",method = RequestMethod.POST)
    public CommonResult add(@RequestBody PmsProductCategoryDto PmsProductCategoryDto){

//        这里是使用Iservice的save 改成使用自定义的save， 是因为不止是当前表，还需要修改其他表.
//                有个当前 商品分类的id 和商品属性的id ，这俩是关系需要记录在pms_product_categoru_attribute_relation 表上。
//            俩表就要有事务防错了
        boolean save = pmsProductCategoryService.cusTomSave(PmsProductCategoryDto);
        return CommonResult.success(save);
    }



// 删除单个
//    url:'/productCategory/delete/'+id,
//    method:'post'
    @RequestMapping(value = "/delete/{id}",method = RequestMethod.POST)
    public CommonResult delete(@PathVariable int id){
        boolean boo = pmsProductCategoryService.removeById(id);
        return CommonResult.success(boo);
    }


//  获取单个数据
//    url:'/productCategory/'+id,
//    method:'get',
        @RequestMapping(value = "/{id}",method = RequestMethod.GET)
        public CommonResult<PmsProductCategoryModel> getOne(@PathVariable int id){
            PmsProductCategoryModel model = pmsProductCategoryService.getById(id);
            return CommonResult.success(model);
        }


    /*
    更新
        url:'/productAttribute/category/update/'+id,
        method:'post',
        data:data
    * */
    @RequestMapping(value = "/update/{id}",method = RequestMethod.POST)
    public CommonResult updateInfo(@PathVariable int id,@RequestBody PmsProductCategoryModel pmsProductCategoryModel){
//        UpdateWrapper<PmsProductCategoryModel> updateWrapper = new UpdateWrapper<>();
//        updateWrapper.lambda().eq(PmsProductCategoryModel::getId,id).set(PmsProductCategoryModel::getName,name);
        boolean b = pmsProductCategoryService.updateById(pmsProductCategoryModel);
        return CommonResult.success(b == true? "更新成功":"更新失败");
    }
}



























